package com.zhanggm.check;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 作业检查
 * Git地址：https://gitee.com/zhanggm1002/workspace-1711d
 */
public class CheckHomeWork {
    /** 作业下载目录 **/
    private static String homeworkDir = "E:\\1711D-作业\\0521\\";
//    private static String homeworkDir = "E:\\1711D-日考录屏\\0522\\";
    /** 学生名单 **/
    private static String[] names = {"牛星元","吕沛哲","王芳","仝道伟","陈奇","吴鹏轩","王留帅","单忠旗","王瑞丹","朱振山","柳亚洲","宋保桢","张涛","张琪","刘国栋","唐建涛","陈轩","刘瑾","杜晨雨","李恒宇","薛永","刘朝鑫","袁纯涛","韩艺馨","李志浩","石启业","师述男","李林","宋英辉","吴泽洲","史登科","张佳伟","龚涵迅","石晓飞","李星德","宋高超","王天硕","武旭芳","汤振洋","申尚峰","王春备","张经杰","王娴辉","成建","刘俊磊","王磊","王潘龙","贺旭东","文彦超","邓颖洁","赵亚","肖文祥","张笑书","张阔","刘星辰","王永帅","张彦龙","郭瑞峰","张文超","王哲"};
    /** 未提交名单 **/
    private static List<String> unSumbitNames = new ArrayList<>();
    /** 待检查名单信息 **/
    private static List<String> checkingNamesInfo = new ArrayList<>();
    /** mp4文件标准大小,8M **/
    private static long Mb = 1024*1024;
    private static long mp4FileSize = Mb*100;
    /** mp4播放时长：分钟 **/
    private static long mp4Time = 2;
    /** 文件修改时间 **/
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMdd HH:mm");
    /**
     * 主函数
     * @param args
     */
    public static void main(String[] args) {
        /** 作业目录 **/
        File dir = new File(homeworkDir);
        File[] files = dir.listFiles();
        /** 按名称检查作业 **/
        for(String name:names){
            boolean isSubmit = false;
            /** 检查是否提交了作业 **/
            for(File file:files){
                /** 找到作业提交的目录 **/
                if(file.getName().contains(name)){
                    /** 检查作业是否合格 **/
                    double totalTime = getTotalTime(file);
                    if(totalTime>0){
                        isSubmit = true;
                        if(totalTime<mp4Time){
                            checkingNamesInfo.add(totalTime+":"+name);
                        }
                        break;
                    }
                }
            }
            /** 未提交作业 **/
            if(!isSubmit){
                unSumbitNames.add(name);
            }
        }
        /** 未提交作业名单 **/
        System.out.println("未提交作业名单:"+unSumbitNames.size());
        System.out.println(unSumbitNames);
        System.out.println();

        System.out.println("待检查名单及上传Mp4文件大小："+checkingNamesInfo.size()+"人");
        Collections.sort(checkingNamesInfo);
        checkingNamesInfo.forEach(info->{
            System.out.println(info);
        });
    }

    /**
     * 检查学生的作业
     * @param name
     * @param homeWorkDir
     * @return
     */
    public static Boolean checkHomeWork(String name, File homeWorkDir){
        final File[] homeworkFiles = homeWorkDir.listFiles();
        if(homeworkFiles==null){
            return false;
        }
        //录屏文件大小，处理多个MP4文件
        float fileSize = 0;
        for (File homeworkFile : homeworkFiles) {
            /** 检查到MP4文件 **/
            if(homeworkFile.getName().endsWith(".mp4")){
                fileSize += (float) homeworkFile.length();
                Map<String, Object> voideMsg = VideoUtils.getMsg(homeworkFile.getPath());
                if(Integer.valueOf(voideMsg.get("time").toString())<10){
                    System.out.println(name+":"+voideMsg);
                }

                /** 文件修改时间 **/
//                long lastModified = homeworkFile.lastModified();
//                Date lastModifiedDate = new Date(lastModified);
//                String isSummitTime = simpleDateFormat.format(lastModifiedDate);
//                System.out.println(isSummitTime);
//                return true;
            }
        }
        /** 如果文件小于8M，则打印信息，重点检查 **/
        if(fileSize<mp4FileSize){
            String fileSizeMb = String.format("%.1f MB", fileSize/Mb);
//                    System.out.println(name+":Mp4FileSize:"+fileSizeMb);
            checkingNamesInfo.add(fileSizeMb+":"+name);
        }
        if(fileSize>0){
            return true;
        }
        return false;
    }

    /**
     * 获取总时长
     * @param filePath
     * @return
     */
    public static double getTotalTime(String filePath){
        File file = new File(filePath);
        return getTotalTime(file);
    }
    public static double getTotalTime(File file){
        List<String> fileList = new ArrayList<>();
        setMp4File(file, fileList);
        double total = 0;
        if(fileList==null){
            return total;
        }
        for (String mp4Path: fileList){
            Map<String, Object> msg = VideoUtils.getMsg(mp4Path);
            if(msg!=null){
                total += Double.valueOf(msg.get("time").toString());
            }
        }
        return total;
    }

    /**
     *
     * @param file
     * @param fileList
     * @return
     */
    public static void setMp4File(File file,List<String> fileList){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for (File mfile:files){
                setMp4File(mfile,fileList);
            }
        }else if(file.getName().endsWith(".mp4")){
            fileList.add(file.getAbsolutePath());
        }
    }




    public static void main1(String[] args) {
        List<String> fileList = new ArrayList<>();
        setMp4File(new File("E:\\1711D-日考录屏\\0518"), fileList);
        System.out.println(fileList);
    }

}
